package com.xiaofeng.pochi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.common.Result;
import com.xiaofeng.pochi.mapper.SysRoleMenuMapper;
import com.xiaofeng.pochi.pojo.LoginUser;
import com.xiaofeng.pochi.pojo.SysRole;
import com.xiaofeng.pochi.pojo.SysRoleMenu;
import com.xiaofeng.pochi.pojo.SysUser;
import com.xiaofeng.pochi.pojo.vo.SysRoleVo;
import com.xiaofeng.pochi.pojo.vo.SysUserVo;
import com.xiaofeng.pochi.service.SysMenuService;
import com.xiaofeng.pochi.service.SysRoleService;
import com.xiaofeng.pochi.utils.IdWorker;
import com.xiaofeng.pochi.utils.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/07/26/21:56
 * @Description:
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 添加
     * @param sysRole
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<?> save(@RequestBody SysRoleVo sysRole) {
        LoginUser loginUser = ShiroUtils.getLoginUser();
        String username = loginUser.getUsername();
        // 创建SysRole对象
        SysRole role = new SysRole();
        // 拷贝属性
        BeanUtils.copyProperties(sysRole, role);
        sysRole.setRoleId(idWorker.nextId());
        role.setCreateBy(username);
        role.setUpdateBy(username);
        sysRoleService.save(role);
        // 下面开始添加角色权限数据
        saveRoleMenu(sysRole, role);
        return new Result<>("添加成功");
    }



    /**
     * 保存角色菜单数据
     * @param sysRole
     * @param role
     */
    private void saveRoleMenu(SysRoleVo sysRole, SysRole role) {
        if(!CollectionUtils.isEmpty(sysRole.getAuthIds())) {
            // 根据菜单ID集合去构建SysRoleMenu
            List<SysRoleMenu> roleMenuList = sysRole.getAuthIds().stream().map(id -> {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                // 设置菜单ID和角色ID
                sysRoleMenu.setMenuId(id);
                sysRoleMenu.setRoleId(role.getRoleId());
                return sysRoleMenu;
            }).collect(Collectors.toList());
            // 存库
            sysRoleMenuMapper.saveBatch(roleMenuList);
        }
    }
    /**
     * 修改
     * @param sysRole
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @Transactional(rollbackFor = Exception.class)
    public Result<?> update(@RequestBody SysRoleVo sysRole) {
        sysRoleService.update(sysRole);
        return new Result<>("修改成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result<?> delete(@PathVariable Long id) {
        sysRoleService.delete(id);
        return new Result<>("删除成功");
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<SysRoleVo> get(@PathVariable Long id) {
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.lambda().eq(SysRole::getRoleId,id);
        SysRole sysRole = sysRoleService.getOne(query);
        // 拷贝属性
        SysRoleVo vo = new SysRoleVo();
        BeanUtils.copyProperties(sysRole, vo);
        //查询这个角色的所有权限
        List<SysRoleMenu> roleMenuList = sysRoleMenuMapper.getByRoleId(id);
        if(!CollectionUtils.isEmpty(roleMenuList)) {
            // 取出权限ID集合
            List<Long> authIds = roleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
            vo.setAuthIds(authIds);
        }
        return new Result<>(vo);
    }


    /**
     * 分页查询
     * @param page
     * @return
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<SysRole>> getByPage(@RequestBody Page<SysRole> page) {
        page = sysRoleService.getByPage(page);
        return new Result<>(page);
    }

    /**
     * 查询所有角色
     * @param
     * @return
     */
    @GetMapping("/findAll")
    public Result<List<SysRole>> getAll() {
        List<SysRole> list = sysRoleService.getAll();
        return new Result<>(list);
    }
}